<?php
/**
 * Created by PhpStorm.
 * User: Ren
 * Date: 4/10/16
 * Time: 4:37 PM
 */

namespace app\controllers;

use yii\web\Controller;
use app\models\Rs_raised;
use app\models\Uc_user;
use app\models\Pr_support;
use Yii;
use app\models\UploadForm;
use yii\web\UploadedFile;
use app\models\Uc_qualification;

class InvapplyController extends Controller
{
    public function actionShow(){

        if(!empty($_COOKIE['user_id'])){
            $user_id = $_COOKIE['user_id'];
            $user = Uc_user::find()->where([
                'user_id' => $user_id,
                'del_flag' => 0
            ])->one();
            if($user['is_investor'] == 1){
                $this->redirect(['qulification/show']);
            }
            $view = Yii::$app->view;
            $view->params['title'] = "申请成为投资人";
            PrlikeController::setView($user);

            $model = new UploadForm();

            if (Yii::$app->request->isPost) {
                $model->file1 = UploadedFile::getInstance($model, 'file1');
                $model->file2 = UploadedFile::getInstance($model, 'file2');
                $model->file3 = UploadedFile::getInstance($model, 'file3');
                if ($model->file1 && $model->file2 && $model->file3 && $model->validate()) {

                    $model->file1->saveAs('uploads/' . $model->file1->baseName . '.' . $model->file1->extension);
                    $model->file2->saveAs('uploads/' . $model->file2->baseName . '.' . $model->file2->extension);
                    $model->file3->saveAs('uploads/' . $model->file3->baseName . '.' . $model->file3->extension);


                    $query = Uc_qualification::findOne([
                        'user_id' => $user_id,
                        'del_flag' => 0,
                    ]);
                    if($query == null){
                        $query = new Uc_qualification();
                        $query['user_id'] = $user_id;
                        $query['frontIDImage'] = 'uploads/' . $model->file1->baseName . '.' . $model->file1->extension;
                        $query['backIDImage'] = 'uploads/' . $model->file2->baseName . '.' . $model->file2->extension;
                        $query['photoImage'] = 'uploads/' . $model->file3->baseName . '.' . $model->file3->extension;
                        $query->insert();
                    }else{
                        $query['frontIDImage'] = 'uploads/' . $model->file1->baseName . '.' . $model->file1->extension;
                        $query['backIDImage'] = 'uploads/' . $model->file2->baseName . '.' . $model->file2->extension;
                        $query['photoImage'] = 'uploads/' . $model->file3->baseName . '.' . $model->file3->extension;
                        $query['type'] = 0;
                        $query->save();
                    }


                    echo "<script>alert('您的申请已经提交...请耐心等待')</script>";
                    echo "<script>location.href = \"http://42.96.185.45/chuangwu/web/index.php?r=qulification/show\";</script>";
                }else{
                    echo "<script>alert('请提交所有材料')</script>";
                    echo "<script>location.href = \"http://42.96.185.45/chuangwu/web/index.php?r=invapply/show\";</script>";
                }

            }{
//                echo "<script>alert('第一种情况')</script>";
                return $this->render('invapply',  ['user'=>$user, 'model'=>$model]);
            }
//            return $this->render('invapply',  ['user'=>$user, 'model'=>$model]);

        }else{
            echo "<script>alert('cookie 不存在')</script>";
            // sleep(3);
            $this->redirect(['login/show']);
        }


    }

    public function actionUpload()
    {
        $model = new UploadForm();

        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');

        if ($model->file && $model->validate()) {
            $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
        }
    }

        return $this->render('upload', ['model' => $model]);
}
}